Skip to content

ENG-1769 Optimize semantic search by removing LIMIT from function body#1141

Open
mdroidian wants to merge 3 commits into
mainfrom
cursor/optimize-semantic-search-eng-1769-cec2
Open

ENG-1769 Optimize semantic search by removing LIMIT from function body#1141
mdroidian wants to merge 3 commits into
mainfrom
cursor/optimize-semantic-search-eng-1769-cec2

Conversation

@mdroidian

@mdroidian mdroidian commented Jun 22, 2026

Copy link
Copy Markdown
Member

Problem

The semantic search functions (match_content_embeddings) were taking much longer than the underlying queries. This performance degradation was caused by the LIMIT being passed as a parameter to the function body, which prevented the PostgreSQL query planner from optimizing the query effectively.

Solution

This PR implements the optimization suggested in ENG-1769:

  1. Removed match_count parameter from match_content_embeddings function
  2. Removed LIMIT clause from the function body to allow the query planner to optimize properly
  3. Moved LIMIT to callers using Supabase's .limit() method on the client side

Changes

Database Changes

  • Updated match_content_embeddings function signature:
    • Removed: match_count integer parameter
    • Removed: LIMIT match_count clause from function body
  • Created migration file to document the change

Application Changes

  • apps/roam/src/utils/hyde.ts: Updated findSimilarNodesVectorOnly to use .limit() method
  • apps/roam/src/utils/discourseNodeSearchProviders.ts: Updated semantic search to use .limit() method
  • packages/database/src/dbTypes.ts: Updated TypeScript types to reflect new function signature

Performance Impact

By removing the LIMIT from the function body, the PostgreSQL query planner can now:

  • Better optimize the vector similarity search
  • Make more informed decisions about index usage
  • Potentially use parallel query execution

The function should now have roughly the same performance as running the underlying query directly.

Testing

The changes maintain backward compatibility in terms of behavior:

  • All existing callers have been updated to apply the limit on the client side
  • The results returned will be identical to before, just with better performance

Linear Issue: ENG-1769

Open in Web Open in Cursor 

- Remove match_count parameter from match_content_embeddings function
- Remove LIMIT clause from function body to improve query planner performance
- Add space_id parameter for better filtering
- Move LIMIT to caller side using Supabase .limit() method
- Update TypeScript types to reflect new function signature

Co-authored-by: Michael Gartner <mdroidian@users.noreply.github.com>
@linear-code

linear-code Bot commented Jun 22, 2026

Copy link
Copy Markdown

ENG-1769

@vercel

vercel Bot commented Jun 22, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
discourse-graph Ready Ready Preview, Comment Jun 22, 2026 3:26pm

Request Review

@supabase

supabase Bot commented Jun 22, 2026

Copy link
Copy Markdown

Updates to Preview Branch (cursor/optimize-semantic-search-eng-1769-cec2) ↗︎

Deployments Status Updated
Database Mon, 22 Jun 2026 15:25:08 UTC
Services Mon, 22 Jun 2026 15:25:08 UTC
APIs Mon, 22 Jun 2026 15:25:08 UTC

Tasks are run on every commit but only new migration files are pushed.
Close and reopen this PR if you want to apply changes from existing seed or migration files.

Tasks Status Updated
Configurations Mon, 22 Jun 2026 15:25:10 UTC
Migrations Mon, 22 Jun 2026 15:25:10 UTC
Seeding Mon, 22 Jun 2026 15:25:10 UTC
Edge Functions Mon, 22 Jun 2026 15:25:14 UTC

View logs for this Workflow Run ↗︎.
Learn more about Supabase for Git ↗︎.

Focus this PR only on the LIMIT optimization that's killing the planner.
The space_id filtering will be addressed in a separate ticket.

Co-authored-by: Michael Gartner <mdroidian@users.noreply.github.com>
@mdroidian mdroidian changed the title Optimize semantic search by removing LIMIT from function body [ENG-1769] ENG-1769 Optimize semantic search by removing LIMIT from function body Jun 22, 2026
@mdroidian mdroidian marked this pull request as ready for review June 22, 2026 04:28
@mdroidian

Copy link
Copy Markdown
Member Author

@codex review

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

Open in Devin Review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 68cd04e7be

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants